<!--
  * 提现申请表
  *
  * @Author:    syf
  * @Date:      2025-05-28 10:25:38
  * @Copyright  畅想科技
-->
<template>
  <a-drawer
      :title="form.txId ? '编辑' : '添加'"
      width="30%"
      :open="visibleFlag"
      @close="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
          <a-row>
                    <a-form-item label="提现申请编号"  name="txId">
                      <a-input-number style="width: 100%" v-model:value="form.txId" placeholder="提现申请编号" />
                    </a-form-item>
                    <a-form-item label="提现用户id"  name="userId">
                      <a-input-number style="width: 100%" v-model:value="form.userId" placeholder="提现用户id" />
                    </a-form-item>
                    <a-form-item label="提现金额"  name="txAmount">
                      <a-input-number style="width: 100%" v-model:value="form.txAmount" placeholder="提现金额" />
                    </a-form-item>
                    <a-form-item label="提现申请状态：0：未审核，1：已审核通过，2：已审核，未通过"  name="txState">
                      <a-input-number style="width: 100%" v-model:value="form.txState" placeholder="提现申请状态：0：未审核，1：已审核通过，2：已审核，未通过" />
                    </a-form-item>
                    <a-form-item label="审核回复"  name="txReply">
                      <a-input style="width: 100%" v-model:value="form.txReply" placeholder="审核回复" />
                    </a-form-item>
                    <a-form-item label="提现审核人"  name="txShr">
                      <a-input style="width: 100%" v-model:value="form.txShr" placeholder="提现审核人" />
                    </a-form-item>
          </a-row>

    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { txApi } from '/@/api/business/tx/tx-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
          txId: undefined,
              txId: undefined, //提现申请编号
              userId: undefined, //提现用户id
              txAmount: undefined, //提现金额
              txState: undefined, //提现申请状态：0：未审核，1：已审核通过，2：已审核，未通过
              txReply: undefined, //审核回复
              txShr: undefined, //提现审核人
  };

  let form = reactive({ ...formDefault });

  const rules = {
                  userId: [{ required: true, message: '提现用户id 必填' }],
                  txAmount: [{ required: true, message: '提现金额 必填' }],
                  txState: [{ required: true, message: '提现申请状态：0：未审核，1：已审核通过，2：已审核，未通过 必填' }],
                  txShr: [{ required: true, message: '提现审核人 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.txId) {
        await txApi.update(form);
      } else {
        await txApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
